package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    @Reference
    private IUserInfoCacheService userInfoCacheService;
    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoMapper.get(phone);
        if (userInfo == null) {
            return true;
        }
        return false;
    }

    @Override
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        AjaxResult result = new AjaxResult();
        try {
        //做外网项目 业务层时必须要做 数据校验
        Assert.hasLength(verifyCode,"验证码不能为空");
        Assert.hasLength(phone,"手机号不能为空");
        Assert.hasLength(nickname,"用户名不能为空");
        Assert.hasLength(password,"密码不能为空");
        Assert.hasLength(rpassword,"确认密码不能为空");
        //数据一致验证
        String verifyCode1 = verifyCodeCacheService.getVerifyCode(phone);
        //数据库中的验证码
        Assert.equalsIgnoreCase(verifyCode,verifyCode1,"验证码不一致");
        Assert.equalsIgnoreCase(password,rpassword,"两次密码输入不一致");
        //封装对象
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setCity("某某地方");
        //保存数据库
        userInfoMapper.insert(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @Override
    public AjaxResult userLogin(String phone, String password) {
        AjaxResult result = new AjaxResult();
            UserInfo userInfo = userInfoMapper.getUserInfo(phone,password);
            if(userInfo==null){//登入失败
                return result.mark("账号或密码不匹配");
            }
            //登入成功生成一个token
            String token = UUID.randomUUID().toString().replace("-", "");
            //缓存对象
            userInfoCacheService.setVerifyCode(token, JSON.toJSONString(userInfo));
            //传递token给控制器
          result.setData(token);
        return result;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
